package com.hbnu.system.controller;


import com.hbnu.system.core.base.CommonPage;
import com.hbnu.system.core.base.Rest;
import com.hbnu.system.model.dto.CheckBackDTO;
import com.hbnu.system.model.dto.WorkDTO;
import com.hbnu.system.model.entity.Work;
import com.hbnu.system.model.entity.Workfile;
import com.hbnu.system.model.query.WorkPageQuery;
import com.hbnu.system.service.IWorkService;
import com.hbnu.system.service.IWorkfileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@RestController
@RequestMapping("/work")
@Api(tags = "著作模块")
public class WorkController {
    @Autowired
    private IWorkService iWorkService;
    @Autowired
    private IWorkfileService workfileService;
    @ApiOperation("著作-正式录入")
    @PostMapping("/addWork")
    public Rest<Boolean> AddWork(@RequestBody Work work){
        boolean add = iWorkService.addnewwork(work);
        return add ? Rest.success() : Rest.failed();
    }

    @ApiOperation("著作-修改")
    @PostMapping("/updateWork")
    public Rest<Boolean> updateWork(@RequestBody Work work){
        boolean add = iWorkService.updatework(work);
        return add ? Rest.success() : Rest.failed();
    }
    @ApiOperation("著作-名称查重")
    @GetMapping("/CheckworkName")
    public Rest<Boolean> CheckWorkName(String name){
        boolean check = iWorkService.checkWorkname(name);
        return check ? Rest.success() : Rest.failed();
    }


    @ApiOperation("著作-删除")
    @GetMapping("/DeleteWork")
    public Rest<Boolean> DeleteWork(String ID) throws IOException {
        boolean delete = iWorkService.deletework(ID);
        return delete ? Rest.success() : Rest.failed();
    }

    @ApiOperation("著作-提交")
    @GetMapping("/ReCommitWork")
    public Rest<Boolean> ReCommitWork(String id){
        boolean add = iWorkService.ReCommitWork(id);
        return add ? Rest.success() : Rest.failed();
    }
    
    @ApiOperation("著作-撤回")
    @GetMapping("/WithdrawWork")
    public Rest<Boolean> WithdrawWork(String id){
        boolean add = iWorkService.withdrawWork(id);
        return add ? Rest.success() : Rest.failed();
    }

    @ApiOperation("根据主键著作查询")
    @GetMapping("/GetWorkById")
    public Rest<Work> GetPaperById(String ID){
        Work work = iWorkService.getWork(ID);
        return Rest.success(work);
    }

    @ApiOperation("根据主键著作DTO查询")
    @GetMapping("/GetWorkDTOById")
    public Rest<WorkDTO> GetPaperDTOById(String ID){
        WorkDTO workdto = iWorkService.getWorkDTOById(ID);
        return Rest.success(workdto);
    }


    @ApiOperation("著作-院级通过")
    @GetMapping("/CollegeCheckPass")
    public Rest<String> CollegePasswork(@RequestHeader("X-Token") String token,String PID){
        Boolean b = iWorkService.collegeCheckPass(token,PID);
        return b ? Rest.success() : Rest.failed();
    }

    @ApiOperation("著作-院级退回")
    @PostMapping("/CollegeCheckBack")
    public Rest<String> CollegeBackwork(@RequestHeader("X-Token") String token,@RequestBody CheckBackDTO checkBackDTO){
        Boolean b = iWorkService.collegeCheckBack(token,checkBackDTO);
        return b ? Rest.success() : Rest.failed();
    }


    @ApiOperation("著作-校级通过")
    @GetMapping("/SchoolCheckPass")
    public Rest<String> SchoolPasswork(@RequestHeader("X-Token") String token,String PID){
        Boolean b = iWorkService.schoolCheckPass(token,PID);
        return b ? Rest.success() : Rest.failed();
    }


    @ApiOperation("著作-校级退回")
    @PostMapping("/SchoolCheckBack")
    public Rest<String> SchoolBackwork(@RequestHeader("X-Token") String token,@RequestBody CheckBackDTO checkBackDTO){
        Boolean b = iWorkService.schoolCheckBack(token,checkBackDTO);
        return b ? Rest.success() : Rest.failed();
    }

    @ApiOperation("用户获取与自己相关的著作-分页")
    @PostMapping("/UserGetRelateWork")
    public Rest<CommonPage<Work>> UserGetRelateWork(@RequestBody WorkPageQuery workPageQuery){
        CommonPage<Work> workCommonPage =iWorkService.userGetRelateWork(workPageQuery);
        return Rest.success(workCommonPage);
    }

    @ApiOperation("著作导出")
    @PostMapping("export")
    public Rest<CommonPage<Work>> export(@RequestBody WorkPageQuery workPageQuery) {
        CommonPage<Work> workCommonPage = iWorkService.export(workPageQuery);
        return Rest.success(workCommonPage);
    }


    @ApiOperation("著作-文件信息上传")
    @PostMapping("/addworkfile")
    public Rest<Boolean> addworkfile(@RequestBody Workfile workfile){
        boolean add = workfileService.addWorkfile(workfile);
        return add ? Rest.success() : Rest.failed();
    }
    @ApiOperation("著作-文件信息删除")
    @GetMapping("/Deleteworkfile")
    public Rest<Boolean> Deleteworkfile(String id)throws IOException {
        boolean delete = workfileService.deletWorkfile(id);
        return delete ? Rest.success() : Rest.failed();
    }
    @ApiOperation("著作-文件信息分页查询")
    @GetMapping ("/pageworkfileCondition")
    public Rest<List<Workfile>> pageworkfileConditionfile(String id){
        List<Workfile> page = workfileService.pageWorkfile(id);
        return Rest.success(page);
    }
}

